package main

import (
	"fmt"
	zabbix "github.com/zabbix/src"
)

func main()  {
	//定义url，zabbix登陆使用的用户名及密码
	url := "http://192.168.73.160/api_jsonrpc.php"
	user := "Admin"
	password := "zabbix"

	api := zabbix.LoginAndHandle(url,user,password)
	defer api.LogOut()

	// 主机信息获取
	hostGet := "host.get"
	hostGetData := `{
        "output": ["hostid"],
        "selectGroups": "extend",
        "filter": {
            "host": [
                "Zabbix server"
            ]
        }
	}`
	hostGetResultOut := api.AllMethodHandles(hostGet,hostGetData)
	fmt.Println(hostGetResultOut)

	// 主机创建
	hostCreate := "host.create"
	hostCreateData := `{
        "host": "Linux server5",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.3.5",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ],
        "inventory_mode": 0,
        "inventory": {
            "macaddress_a": "01234",
            "macaddress_b": "56768"
        }
	}`
	hostCreateResultOut := api.AllMethodHandles(hostCreate,hostCreateData)
	fmt.Println(hostCreateResultOut)

	// 主机删除
	hostDelete := "host.delete"
	hostDeleteData := `[
     	"10084",
        "10084"
	]`
	hostDeleteResultOut := api.AllMethodHandles(hostDelete,hostDeleteData)
	fmt.Println(hostDeleteResultOut)

	// 主机更新
	hostUpdate := "host.update"
	hostUpdateData := `{
        "hostid": "10126",
        "status": 0
	}`
	hostUpdateResultOut := api.AllMethodHandles(hostUpdate,hostUpdateData)
	fmt.Println(hostUpdateResultOut)

	// 监控项获取
	itemGet := "item.get"
	itemGetData := `{
        "output": "extend",
        "hostids": "10263",
        "search": {
            "key_": "system"
        },
        "sortfield": "name"
	}`
	itemGetResultOut := api.AllMethodHandles(itemGet,itemGetData)
	fmt.Println(itemGetResultOut)

	// 监控项创建
	itemCreate := "item.create"
	itemCreateData := `{
        "name": "Free disk space on $1",
        "key_": "vfs.fs.size[/home/joe/,free]",
        "hostid": "30074",
        "type": 0,
        "value_type": 3,
        "interfaceid": "30084",
        "applications": [
            "609",
            "610"
        ],
        "delay": "30s"
	}`
	itemCreateResultOut := api.AllMethodHandles(itemCreate,itemCreateData)
	fmt.Println(itemCreateResultOut)

	// 监控项删除
	itemDelete := "item.delete"
	itemDeleteData := `[
        "22982",
        "22986"
	]`
	itemDeleteResultOut := api.AllMethodHandles(itemDelete,itemDeleteData)
	fmt.Println(itemDeleteResultOut)

	// 监控项更新
	itemUpdate := "item.update"
	itemUpdateData := `{
        "itemid": "10092",
        "status": 0
	}`
	itemUpdateResultOut := api.AllMethodHandles(itemUpdate,itemUpdateData)
	fmt.Println(itemUpdateResultOut)

	// 触发器获取
	triggerGet := "trigger.get"
	triggerGetData := `{
        "triggerids": "16040",
        "output": "extend",
        "selectFunctions": "extend"
	}`
	triggerGetResultOut := api.AllMethodHandles(triggerGet,triggerGetData)
	fmt.Println(triggerGetResultOut)

	// 触发器创建
	triggerCreate := "trigger.create"
	triggerCreateData := `[
        {
            "description": "Processor load is too high on {HOST.NAME}",
            "expression": "{Linux server:system.cpu.load[percpu,avg1].last()}>5",
            "dependencies": [
                {
                    "triggerid": "17367"
                }
            ]
        },
        {
            "description": "Service status",
            "expression": "{Linux server:log[/var/log/system,Service .* has stopped].strlen()}<>0",
            "dependencies": [
                {
                    "triggerid": "17368"
                }
            ],
            "tags": [
                {
                    "tag": "service",
                    "value": "{{ITEM.VALUE}.regsub(\"Service (.*) has stopped\", \"\\1\")}"
                },
                {
                    "tag": "error",
                    "value": ""
                }
            ]
        }
	]`
	triggerCreateResultOut := api.AllMethodHandles(triggerCreate,triggerCreateData)
	fmt.Println(triggerCreateResultOut)

	// 触发器删除
	triggerDelete := "trigger.delete"
	triggerDeleteData := `[
        "12002",
        "12003"
	]`
	triggerDeleteResultOut := api.AllMethodHandles(triggerDelete,triggerDeleteData)
	fmt.Println(triggerDeleteResultOut)

	// 触发器更新
	triggerUpdata := "trigger.update"
	triggerUpdateData := `{
        "triggerid": "16040",
        "status": 1
	}`
	triggerUpdateResultOut := api.AllMethodHandles(triggerUpdata,triggerUpdateData)
	fmt.Println(triggerUpdateResultOut)
}

